package codechicken.wirelessredstone.core;

import net.minecraft.src.*;

public abstract class RedstoneEtherCommandHandler
{
	public static boolean handleCommand(String commandstring, String playername, ICommandListener icommandlistener, boolean playerOP)
	{
		String[] parameterarray = commandstring.toLowerCase().split(" ");
		
		if(!parameterarray[0].equals("freq") && !parameterarray[0].equals("frequency"))
		{
			return false;
		}
		
		if(!playerOP)
		{
			icommandlistener.log("Not an OP");
			return true;
		}

		if(parameterarray.length != 3 && parameterarray.length != 4)
		{
			icommandlistener.log("Invalid command");
			return true;
		}

		if(parameterarray[1].equals("public"))
		{
			int newfreqend;
			try
			{
				newfreqend = Integer.parseInt(parameterarray[2]);
			}
			catch(NumberFormatException ne)
			{
				icommandlistener.log("Invalid Frequency.");
				return true;
			}

			if(newfreqend < 0 || newfreqend > RedstoneEther.numfreqs)
			{
				icommandlistener.log("Invalid Frequency.");
				return true;
			}

			RedstoneEther.setLastPublicFrequency(newfreqend);
			ModLoaderMp.sendChatToOps(playername, (new StringBuilder()).append("The public frequency has been set to: 0-").append(RedstoneEther.getLastPublicFrequency()).append(".").toString());
			return true;
		}

		if(parameterarray[1].equals("jam") || parameterarray[1].equals("open"))
		{
			if(parameterarray.length != 4)
			{
				icommandlistener.log("Invalid number of Arguments.");
				return true;
			}

			boolean jamfreq = parameterarray[1].equals("jam");

			EntityPlayerMP player = ModLoader.getMinecraftServerInstance().configManager.getPlayerEntity(parameterarray[2]);
			if(player == null)
			{
				icommandlistener.log("No such Player.");
				return true;
			}

			int startfreq;
			int endfreq;

			if(parameterarray[3].equals("all"))
			{
				startfreq = 1;
				endfreq = RedstoneEther.numfreqs;
			}
			else
			{
				int[] freqrange = RedstoneEther.parseFrequencyRange(parameterarray[3]);
				startfreq = freqrange[0];
				endfreq = freqrange[1];
			}

			if(startfreq < 1 || endfreq > RedstoneEther.numfreqs || endfreq < startfreq)
			{
				icommandlistener.log("Invalid Frequency Range.");
				return true;
			}

			RedstoneEther.setFrequencyRangeCommand(player, startfreq, endfreq, jamfreq);

			StringBuilder returnstring = (new StringBuilder());

			if(jamfreq)
			{
				returnstring.append("jammed from");
			}
			else
			{
				returnstring.append("granted");
			}

			if(startfreq == endfreq)
			{
				if(startfreq <= RedstoneEther.getLastPublicFrequency())
				{
					return true;
				}
				returnstring.append(" frequency: ").append(startfreq).append(".");
			}
			else
			{
				int publicend = RedstoneEther.getLastPublicFrequency();
				if(startfreq <= publicend && endfreq <= publicend)
				{
					return true;
				}
				if(startfreq <= publicend)
				{
					startfreq = publicend + 1;
				}
				returnstring.append(" frequencies: ").append(startfreq).append("-").append(endfreq).append(".");
			}

			String opstring = ((new StringBuilder()).append(player.username).append(" was ").append(returnstring).toString());
			String playerstring = ((new StringBuilder()).append("You have been ").append(returnstring).toString());

			ModLoaderMp.sendChatToOps(playername, opstring);
			player.playerNetServerHandler.sendPacket(new Packet3Chat(playerstring));

			return true;

		}

		if(parameterarray[1].equals("scan"))
		{
			if(parameterarray[2].equals("public"))
			{
				icommandlistener.log((new StringBuilder()).append("The public frequency range is: 0-").append(RedstoneEther.getLastPublicFrequency()).append(".").toString());
				return true;
			}

			EntityPlayerMP player = ModLoader.getMinecraftServerInstance().configManager.getPlayerEntity(parameterarray[2]);
			if(player == null)
			{
				icommandlistener.log("No such Player.");
				return true;
			}

			StringBuilder returnstring = (new StringBuilder()).append(player.username);
			int ranges = 0;
			int startfreq;
			int endfreq = RedstoneEther.getLastPublicFrequency();
			while(true)
			{
				int[] freqrange = RedstoneEther.getNextOpenFrequencyRange(player, endfreq + 1);
				startfreq = freqrange[0];
				endfreq = freqrange[1];
				if(startfreq == -1)
				{
					break;
				}

				if(ranges == 0)
				{
					returnstring.append(" can broadcast on: ");
				}
				else
				{
					returnstring.append(",");
				}

				if(startfreq == endfreq)
				{
					returnstring.append(startfreq).append(",");
				}
				else
				{
					returnstring.append(startfreq).append("-").append(endfreq);
				}

				ranges++;

				if(endfreq == RedstoneEther.numfreqs)
				{
					break;
				}
			}

			if(ranges == 0)
			{
				icommandlistener.log(returnstring.append(" can only broadcast on public frequencies.").toString());
			}
			else
			{
				icommandlistener.log(returnstring.append(".").toString());
			}
			return true;
		}

		if(parameterarray[1].equals("gui") && playername.equals("CONSOLE"))
		{
			if(parameterarray[2].equals("on"))
			{
				if(RedstoneEtherGui.guiOn())
				{
					icommandlistener.log("Gui is already on");
					return true;
				}
				RedstoneEtherGui.openGui();
			}
			else if(parameterarray[2].equals("off"))
			{
				if(!RedstoneEtherGui.guiOn())
				{
					icommandlistener.log("Gui is already off");
					return true;
				}
				RedstoneEtherGui.closeGui();
			}
			else
			{
				icommandlistener.log("Invalid command");
			}
			return true;
		}
		
		icommandlistener.log("Invalid command");
		return true;
	}
}
